package LeetCode;

public class LC_098_ValidateBinarySearchTree {
    public static void main(String[] args) {

    }

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    static class Solution {
        public boolean isValidBST(TreeNode root) {
            return isValidBST(root, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
        }

        boolean isValidBST(TreeNode p, double min, double max) {
            if (p == null)
                return true;

            if (p.val <= min || p.val >= max)
                return false;

            return isValidBST(p.left, min, p.val) && isValidBST(p.right, p.val, max);
        }
    }

    static class Solution_2 {
        public boolean isValidBST(TreeNode root) {
            return isValidBST(root, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
        }

        boolean isValidBST(TreeNode p, double min, double max) {
            return p == null || !(p.val <= min) && !(p.val >= max) && isValidBST(p.left, min, p.val) && isValidBST(p.right, p.val, max);
        }
    }
}
